package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.LogBean;
import com.project.dao.ILogDao;
import com.project.service.ILogService;
import com.project.util.InfoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author lyl
 * description:
 */
@Service
@Transactional
public class LogServiceImpl implements ILogService {
    @Autowired
    private ILogDao iLogDao;

    @Override
    public InfoResult addLog(String username) {
        LogBean l = new LogBean();
        DateTimeFormatter dfDate = DateTimeFormatter.ofPattern("HH 时 mm 分 ss 秒");
        l.setLogDate(LocalDate.now());
        l.setLogContent("用户" + username + "在" + dfDate.format(LocalDateTime.now()) + "进行登录操作");
        int x = iLogDao.insert(l);
        if (x != 1) {
            return new InfoResult(1, "添加日志失败");
        }
        return new InfoResult(0, "添加日志成功");
    }

    @Override
    public PageInfo<LogBean> showLogByPage(int pageNum, LocalDate startDate, LocalDate endDate) {
        PageHelper.startPage(pageNum, 3);
        PageInfo<LogBean> info = PageInfo.of(iLogDao.showLogByPage(startDate, endDate));
        return info;
    }
}
